//
//  XTPopTableView.m
//  XTPopView
//
//  Created by zjwang on 16/7/7.
//  Copyright © 2016年 夏天. All rights reserved.
//

#import "XTPopTableView.h"
#import "XTPopTableViewCell.h"

@interface XTPopTableView ()

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation XTPopTableView
- (instancetype)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(CGFloat)height Type:(XTDirectionType)type Color:(UIColor *)color
{
    if ([super initWithOrigin:CGPointMake(origin.x, origin.y+10) Width:width Height:height Type:type Color:color]) {
        // 添加tableview
        [self.backGoundView addSubview:self.tableView];
        self.backGoundView.layer.cornerRadius = 4;
        self.backGoundView.clipsToBounds = YES;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    return self;
}

#pragma mark -
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.backGoundView.frame.size.width, self.backGoundView.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"XTPopTableViewCell" bundle:nil] forCellReuseIdentifier:@"XTPopTableViewCell"];
    }
    return _tableView;
}
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
#pragma mark -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.row_height == 0) {
        return 44;
    }else{
        return self.row_height;
    }
}
#pragma mark -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTPopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XTPopTableViewCell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.iconView.image = [UIImage imageNamed:self.images[indexPath.row]];
    cell.valueLabel.text = self.dataArray[indexPath.row];
    cell.valueLabel.font = [UIFont systemFontOfSize:self.fontSize];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.valueLabel.textColor = self.titleTextColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    
    if (self.dataArray.count == 1) {
        self.tableView.bounces = NO;
        self.tableView.separatorColor = [UIColor clearColor];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexPathRow:)]) {
        [self removeFromSuperview];
        [self.delegate selectIndexPathRow:indexPath.row];
    }
}

+ (void)showPopViewByCenter:(UIView *)view valueList:(NSArray *)valueList delegate:(id)obj
{
    CGRect rect = [view convertRect:view.bounds toView:[AppDelegate defaultService].window];
    CGPoint point           = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height);
    XTPopTableView *popView = [[XTPopTableView alloc] initWithOrigin:point Width:150 Height:40*valueList.count - 2 Type:XTTypeOfUpCenter Color:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    popView.dataArray       = valueList;
    popView.row_height      = 40;
    popView.delegate        = obj;
    popView.titleTextColor  = [UIColor lightGrayColor];
    popView.fontSize        = 15.f;
    [popView popView];
}

+ (void)showPopViewByCenter:(UIView *)view valueList:(NSArray *)valueList imageList:(NSArray *)imageList delegate:(id)obj
{
    CGRect rect = [view convertRect:view.bounds toView:[AppDelegate defaultService].window];
    CGPoint point           = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height);
    XTPopTableView *popView = [[XTPopTableView alloc] initWithOrigin:point Width:150 Height:40*valueList.count - 2 Type:XTTypeOfUpCenter Color:[UIColor whiteColor]];
    popView.dataArray       = valueList;
    popView.images          = imageList;
    popView.row_height      = 40;
    popView.delegate        = obj;
    popView.titleTextColor  = [UIColor lightGrayColor];
    popView.fontSize        = 15.f;
    [popView popView];
}

+ (void)showPopViewBottomByCenter:(UIView *)view valueList:(NSArray *)valueList delegate:(id)obj
{
    CGRect rect = [view convertRect:view.bounds toView:[AppDelegate defaultService].window];
    CGPoint point           = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y);
    XTPopTableView *popView = [[XTPopTableView alloc] initWithOrigin:point Width:150 Height:40*valueList.count - 2 Type:XTTypeOfDownCenter Color:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    popView.dataArray       = valueList;
    popView.row_height      = 40;
    popView.delegate        = obj;
    popView.titleTextColor  = [UIColor lightGrayColor];
    popView.fontSize        = 15.f;
    [popView popView];
}

+ (void)removePopView
{
    XTPopTableView *popView = [[XTPopTableView alloc] init];
    [popView dismissPopView];
}

- (void)dealloc
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(closePopView)]) {
        [self.delegate closePopView];
    }
}
@end
